<template>
  <div class="app-container">
    <div class="div-sch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="访客ID" prop="uId">
          <el-input
            v-model="queryParams.uId"
            placeholder="请输入访客ID"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="访客姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入访客姓名"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="审批状态" prop="approveStatus">
          <el-select
            v-model="queryParams.approveStatus"
            placeholder="请选择状态"
            clearable
            style="width: 150px"
          >
            <el-option :value="0" label="待审核"></el-option>
            <el-option :value="1" label="通过"></el-option>
            <el-option :value="2" label="不通过"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="div-t">
      <el-table
        ref="tables"
        border
        v-loading="loading"
        :data="list"
        @selection-change="handleSelectionChange"
        :default-sort="defaultSort"
        @sort-change="handleSortChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="访客ID" minWidth="150%" align="center" prop="uId" v-if="false" />
        <el-table-column label="访客姓名" align="center" prop="visitorName" />
        <el-table-column label="被访公司" align="center" prop="deptName" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="来访目的" align="center" prop="purpose" />
        <el-table-column label="审批状态" align="center" prop="approveStatus">
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.approveStatus == 0
                  ? ''
                  : scope.row.approveStatus == 1
                  ? 'success'
                  : 'danger'
              "
            >
              {{
              scope.row.approveStatus == 0
              ? "待审核"
              : scope.row.approveStatus == 1
              ? "通过"
              : "不通过"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="开始时间" align="center" prop="startTime" />
        <el-table-column label="结束时间" align="center" prop="endTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              :icon="scope.row.approveStatus==0?'el-icon-edit':'el-icon-view'"
              @click="handleUpdate(scope.row)"
            >{{scope.row.approveStatus==0?'审核':'查看'}}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 审核对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-row :gutter="10">
          <el-col :span="24" :xs="24">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>访客信息</span>
              </div>
              <div>
                <div class="text-center">
                  <el-badge value="VIP" :hidden="!form.isVip">
                    <el-image
                      style="height: 150px;width: 150px;border-radius: 100%;"
                      lazy
                      :src="img"
                      fit="fill"
                      :preview-src-list="[img]"
                    ></el-image>
                  </el-badge>
                </div>
                <ul class="list-group list-group-striped">
                  <li class="list-group-item">
                    <svg-icon icon-class="user" />访客姓名
                    <div class="pull-right">{{ form.visitorName || '--' }}</div>
                  </li>
                  <li class="list-group-item">
                    <svg-icon icon-class="phone" />访客手机
                    <div class="pull-right">{{ form.phone || '--' }}</div>
                  </li>
                  <li class="list-group-item">
                    <svg-icon icon-class="email" />来访目的
                    <div class="pull-right">{{ form.purpose || '--' }}</div>
                  </li>
                  <li class="list-group-item">
                    <svg-icon icon-class="user" />被访员工
                    <div class="pull-right">
                      {{ form.staffName || '--' }}
                      <span
                        v-if="form.staffPhone"
                      >/ {{ form.staffPhone }}</span>
                    </div>
                  </li>
                  <li class="list-group-item">
                    <svg-icon icon-class="date" />拜访日期
                    <div class="pull-right">{{ form.startTime }} - {{ form.endTime }}</div>
                  </li>
                </ul>
              </div>
            </el-card>
          </el-col>

          <el-col :span="24" :xs="24">
            <el-card class="box-card" style="margin-top:10px;">
              <div slot="header" class="clearfix">
                <span>防疫审核结果</span>
              </div>
              <div>
                <ul class="list-group list-group-striped">
                  <li class="list-group-item">
                    <svg-icon icon-class="health" />健康码
                    <div class="pull-right">{{ form.hthResult || '--' }}</div>
                    <el-tooltip class="item" effect="dark" :content="form.healthSet || '无通行规则'" placement="right">
                      <i class="el-icon-info"></i>
                    </el-tooltip>
                  </li>
                  <li class="list-group-item">
                    <svg-icon icon-class="trip" />行程码
                    <div class="pull-right">{{ form.tripResult || '--' }}</div>
                    <el-tooltip class="item" effect="dark" :content="form.tripSet || '无通行规则'" placement="right">
                      <i class="el-icon-info"></i>
                    </el-tooltip>
                  </li>
                  <li class="list-group-item">
                    <svg-icon icon-class="skill" />核酸码
                    <div class="pull-right">{{ form.nucleicResult || '--' }}</div>
                    <el-tooltip class="item" effect="dark" :content="form.nucleicSet || '无通行规则'" placement="right">
                      <i class="el-icon-info"></i>
                    </el-tooltip>
                  </li>
                </ul>
              </div>
            </el-card>
          </el-col>

          <el-col :span="8" style="margin-top:10px;">
            <el-card :body-style="{ padding: '5px' }">
              <el-image
                style="height: 100px;width: 100%;"
                lazy
                :src="healthImg"
                fit="contain"
                :preview-src-list="[healthImg]"
              ></el-image>
              <div style="padding: 5px;">
                <time class="time">健康码</time>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8" style="margin-top:10px;">
            <el-card :body-style="{ padding: '5px' }">
              <el-image
                style="height: 100px;width: 100%;"
                lazy
                :src="nucleicImg"
                fit="contain"
                :preview-src-list="[nucleicImg]"
              ></el-image>
              <div style="padding: 5px;">
                <time class="time">核酸码</time>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8" style="margin-top:10px;">
            <el-card :body-style="{ padding: '5px' }">
              <el-image
                style="height: 100px;width: 100%;"
                lazy
                :src="tripImg"
                fit="contain"
                :preview-src-list="[tripImg]"
              ></el-image>
              <div style="padding: 5px;">
                <time class="time">行程码</time>
              </div>
            </el-card>
          </el-col>
        </el-row>
        <el-form ref="form" :model="form" label-position="left" :rules="rules" label-width="100px">
          <el-form-item label="审核结果" prop="approveStatus" style="margin-top:10px;">
            <el-radio-group v-model="form.approveStatus" size="medium">
              <el-radio
                v-for="(item, index) in approveStatusOptions"
                :key="index"
                :label="item.value"
                :disabled="item.disabled"
              >{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" :disabled="!this.approveStatusTemp==0">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getTagList,
  addTag,
  updateTag,
  batchDeleteTag,
  getTagAllList
} from "@/api/business/tag";
import { visitorApi } from "@/api/business/visitor";
import AddOrUpdate from "./visitor-add-update";
import userAvatar from "@/views/system/user/profile/userAvatar";
import badImg from "@/assets/images/bad_img.jpg";

export default {
  name: "visitor",
  components: {
    AddOrUpdate,
    userAvatar
  },
  dicts: ["sys_oper_type", "sys_common_status"],
  data() {
    return {
      tableHeight: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      deleteParam: {
        ids: [],
        staffIds: []
      },
      // 下拉框标签数组
      tagList: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: "operTime", order: "descending" },
      // 弹出层标题
      title: "",
      form: {},
      approveStatusOptions: [
        {
          label: "通过",
          value: 1
        },
        {
          label: "不通过",
          value: 2
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isAsc: "desc",
        uId: undefined,
        name: undefined,
        phone: undefined,
        approveStatus: undefined
      },
      // 表单校验
      rules: {
        approveStatus: [
          { required: true, message: "请选择审批结果！", trigger: "blur" }
        ]
      },
      approveStatusTemp: null,
      visitorImg: "",
      healthImg: "",
      nucleicImg: "",
      tripImg: ""
    };
  },
  filters: {
    roleGroup(data) {
      if (data) {
        let arr = [];
        data.forEach(item => {
          arr.push(item.roleName);
        });
        return arr.join(",");
      }
      return data;
    }
  },
  created() {
    this.init();
    this.getList();
    this.getTableHeight();
  },
  methods: {
    getImageJpg(row) {
      visitorApi.getImage(row.img).then(res => {
        if (res) {
          this.img = `data:image/jpeg;base64,${res.replace(
            /[\r\n]/g,
            ""
          )}`;
        }
      });
      visitorApi.getImage(row.hthCode).then(res => {
        if (res) {
          this.healthImg = `data:image/jpeg;base64,${res.replace(
            /[\r\n]/g,
            ""
          )}`;
        }
      });
      visitorApi.getImage(row.nucleicCode).then(res => {
        if (res) {
          this.nucleicImg = `data:image/jpeg;base64,${res.replace(
            /[\r\n]/g,
            ""
          )}`;
        }
      });
      visitorApi.getImage(row.tripCode).then(res => {
        if (res) {
          this.tripImg = `data:image/jpeg;base64,${res.replace(/[\r\n]/g, "")}`;
        }
      });
    },
    init() {
      this.getTableHeight();
      getTagAllList()
        .then(res => {
          this.tagList = res.data;
        })
        .catch(error => {
          console.log(error);
        });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 300;
    },
    /** 查询 */
    getList() {
      this.loading = true;
      visitorApi
        .getVisitList(this.queryParams)
        .then(res => {
          if (res.data) {
            this.list = res.data.records;
            this.total = res.data.total;
          }
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.deleteParam.ids = selection.map(item => item.id);
      this.deleteParam.staffIds = selection.map(item => item.staffId);
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },

    /** 审核按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "访客审核";
      this.form = this.deepClone(row);
      this.open = true;
      this.approveStatusTemp = this.deepClone(row).approveStatus;
      this.getImageJpg(row);
    },

    // 表单重置
    reset() {
      this.form = {};
      this.healthImg = "";
      this.nucleicImg = "";
      this.tripImg = "";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.getList();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          visitorApi
            .chooseApproveStatus({
              approveStatus: this.form.approveStatus,
              uId: this.form.uId
            })
            .then(res => {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            })
            .catch(error => {
              console.log(error);
            });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.list-group{
  margin: 0;
}
.list-group-item {
  .svg-icon {
    vertical-align: middle !important;
    margin-right: 10px !important;
  }
}
.list-group-item:first-child{
  border-top: none;
}
.list-group-item:last-child{
  border-bottom: none;
}
</style>
